﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using GtsServer.Common;
using System.IO;

namespace GtsServer.Content.Service
{
    public class Service : IService
    {
        string contentPath = ConfigurationManager.AppSettings["ContentPath"].ToString();

        public bool SaveContent(byte[] contentByte, string fileName, string oldFileName)
        {
            string path = contentPath + "\\Content\\Image\\";
            if (fileName.Split('.')[1].ToLower() == "apk")//版本
                path = contentPath + "\\Content\\Version\\";

            if (oldFileName != "")
            {
                if (System.IO.File.Exists(path + oldFileName))
                    System.IO.File.Delete(path + oldFileName);
            }

            try
            {
                FileStream fs = new FileStream(path + fileName, FileMode.Create, FileAccess.Write);
                fs.Write(contentByte, 0, contentByte.Length);
                fs.Flush();
                fs.Close();
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("异常：", ex);
            }
            return false;
        }


        public bool DeleteByFileName(string oldFileName)
        {
            string path = contentPath + "\\Content\\Image\\";
            if (oldFileName.Split('.')[1].ToLower() == "apk")//版本
                path = contentPath + "\\Content\\Version\\";

            try
            {
                if (System.IO.File.Exists(path + oldFileName))
                    System.IO.File.Delete(path + oldFileName);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
